home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / tabdlg2.zip / TEST / GENERIC.C < prev    next >
C/C++ Source or Header  |  1994-05-17  |  12KB  |  310 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Generic.c
  4.  
  5.     PURPOSE: Generic template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.         Windows can have several copies of your application running at the
  18.         same time.  The variable hInst keeps track of which instance this
  19.         application is so that processing will be to the correct window.
  20.  
  21. ****************************************************************************/
  22. #define STRICT
  23. #include <windows.h>            /* required for all Windows applications */
  24. #include "generic.h"            /* specific to this program          */
  25.  
  26. #include "resource.h"
  27. #include "tabdlg2.h"
  28.  
  29. HANDLE hInst;               /* current instance              */
  30.  
  31.  
  32.  
  33. /****************************************************************************
  34.  
  35.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  36.  
  37.     PURPOSE: calls initialization function, processes message loop
  38.  
  39.     COMMENTS:
  40.  
  41.         Windows recognizes this function by name as the initial entry point
  42.         for the program.  This function calls the application initialization
  43.         routine, if no other instance of the program is running, and always
  44.         calls the instance initialization routine.  It then executes a message
  45.         retrieval and dispatch loop that is the top-level control structure
  46.         for the remainder of execution.  The loop is terminated when a WM_QUIT
  47.         message is received, at which time this function exits the application
  48.         instance by returning the value passed by PostQuitMessage().
  49.  
  50.         If this function must abort before entering the message loop, it
  51.         returns the conventional value NULL.
  52.  
  53. ****************************************************************************/
  54.  
  55. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  56. HINSTANCE hInstance;                /* current instance         */
  57. HINSTANCE hPrevInstance;            /* previous instance        */
  58. LPSTR lpCmdLine;                 /* command line             */
  59. int nCmdShow;                    /* show-window type (open/icon) */
  60. {
  61.     MSG msg;                     /* message              */
  62.  
  63.     if (!hPrevInstance)          /* Other instances of app running? */
  64.     if (!InitApplication(hInstance)) /* Initialize shared things */
  65.         return (FALSE);      /* Exits if unable to initialize     */
  66.  
  67.     /* Perform initializations that apply to a specific instance */
  68.  
  69.     if (!InitInstance(hInstance, nCmdShow))
  70.         return (FALSE);
  71.  
  72.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  73.  
  74.     while (GetMessage(&msg,    /* message structure              */
  75.         NULL,          /* handle of window receiving the message */
  76.         NULL,          /* lowest message to examine          */
  77.         NULL))         /* highest message to examine         */
  78.     {
  79.     TranslateMessage(&msg);    /* Translates virtual key codes       */
  80.     DispatchMessage(&msg);     /* Dispatches message to window       */
  81.     }
  82.  
  83.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  84. }
  85.  
  86.  
  87. /****************************************************************************
  88.  
  89.     FUNCTION: InitApplication(HANDLE)
  90.  
  91.     PURPOSE: Initializes window data and registers window class
  92.  
  93.     COMMENTS:
  94.  
  95.         This function is called at initialization time only if no other
  96.         instances of the application are running.  This function performs
  97.         initialization tasks that can be done once for any number of running
  98.         instances.
  99.  
  100.         In this case, we initialize a window class by filling out a data
  101.         structure of type WNDCLASS and calling the Windows RegisterClass()
  102.         function.  Since all instances of this application use the same window
  103.         class, we only need to do this when the first instance is initialized.
  104.  
  105.  
  106. ****************************************************************************/
  107.  
  108. BOOL InitApplication(hInstance)
  109. HINSTANCE hInstance;                  /* current instance       */
  110. {
  111.     WNDCLASS  wc;
  112.  
  113.     /* Fill in window class structure with parameters that describe the       */
  114.     /* main window.                                                           */
  115.  
  116.     wc.style = NULL;                    /* Class style(s).                    */
  117.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  118.                                         /* windows of this class.             */
  119.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  120.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  121.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  122.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  123.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  124.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  125.     wc.lpszMenuName =  "GenericMenu";   /* Name of menu resource in .RC file. */
  126.     wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */
  127.  
  128.     /* Register the window class and return success/failure code. */
  129.  
  130.     return (RegisterClass(&wc));
  131.  
  132. }
  133.  
  134.  
  135. /****************************************************************************
  136.  
  137.     FUNCTION:  InitInstance(HANDLE, int)
  138.  
  139.     PURPOSE:  Saves instance handle and creates main window
  140.  
  141.     COMMENTS:
  142.  
  143.         This function is called at initialization time for every instance of
  144.         this application.  This function performs initialization tasks that
  145.         cannot be shared by multiple instances.
  146.  
  147.         In this case, we save the instance handle in a static variable and
  148.         create and display the main program window.
  149.  
  150. ****************************************************************************/
  151.  
  152. BOOL InitInstance(hInstance, nCmdShow)
  153.     HINSTANCE          hInstance;          /* Current instance identifier.       */
  154.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  155. {
  156.     HWND            hWnd;               /* Main window handle.                */
  157.  
  158.     /* Save the instance handle in static variable, which will be used in  */
  159.     /* many subsequence calls from this application to Windows.            */
  160.  
  161.     hInst = hInstance;
  162.  
  163.     /* Create a main window for this application instance.  */
  164.  
  165.     hWnd = CreateWindow(
  166.         "GenericWClass",                /* See RegisterClass() call.          */
  167.         "Generic Sample Application",   /* Text for window title bar.         */
  168.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  169.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  170.         CW_USEDEFAULT,                  /* Default vertical position.         */
  171.         CW_USEDEFAULT,                  /* Default width.                     */
  172.         CW_USEDEFAULT,                  /* Default height.                    */
  173.         NULL,                           /* Overlapped windows have no parent. */
  174.         NULL,                           /* Use the window class menu.         */
  175.         hInstance,                      /* This instance owns this window.    */
  176.         NULL                            /* Pointer not needed.                */
  177.     );
  178.  
  179.     /* If window could not be created, return "failure" */
  180.  
  181.     if (!hWnd)
  182.         return (FALSE);
  183.  
  184.     /* Make the window visible; update its client area; and return "success" */
  185.  
  186.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  187.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  188.     return (TRUE);               /* Returns the value from PostQuitMessage */
  189.  
  190. }
  191.  
  192. /****************************************************************************
  193.  
  194.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  195.  
  196.     PURPOSE:  Processes messages
  197.  
  198.     MESSAGES:
  199.  
  200.     WM_COMMAND    - application menu (About dialog box)
  201.     WM_DESTROY    - destroy window
  202.  
  203.     COMMENTS:
  204.  
  205.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  206.     current instance address of the About() function.  Then call Dialog
  207.     box which will create the box according to the information in your
  208.     generic.rc file and turn control over to the About() function.  When
  209.     it returns, free the intance address.
  210.  
  211. ****************************************************************************/
  212.  
  213. long CALLBACK __export MainWndProc(hWnd, message, wParam, lParam)
  214. HWND hWnd;                      /* window handle                 */
  215. UINT message;                   /* type of message               */
  216. WPARAM wParam;                  /* additional information        */
  217. LPARAM lParam;                  /* additional information        */
  218. {
  219.  
  220.     switch (message)
  221.     {
  222.         case WM_CREATE:
  223.  
  224.         return 0;
  225.         
  226.         case WM_COMMAND:       /* message: command from application menu */
  227.         switch(wParam)
  228.         {
  229.          case IDM_ABOUT:
  230.           DialogBox(hInst,        /* current instance          */
  231.                     "AboutBox",         /* resource to use           */
  232.                     hWnd,               /* parent handle             */
  233.                     About);             /* About() instance address  */
  234.          return 0;
  235.         }
  236.         break;
  237.  
  238.         case WM_DESTROY:          /* message: window being destroyed */
  239.             PostQuitMessage(0);
  240.             return 0;
  241.  
  242.         default:                  /* Passes it on if unproccessed    */
  243.         break;
  244.        
  245.     }
  246.    return (DefWindowProc(hWnd, message, wParam, lParam));
  247. }
  248.  
  249.  
  250. /****************************************************************************
  251.  
  252.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  253.  
  254.     PURPOSE:  Processes messages for "About" dialog box
  255.  
  256.     MESSAGES:
  257.  
  258.     WM_INITDIALOG - initialize dialog box
  259.     WM_COMMAND    - Input received
  260.  
  261.     COMMENTS:
  262.  
  263.     No initialization is needed for this particular dialog box, but TRUE
  264.     must be returned to Windows.
  265.  
  266.     Wait for user to click on "Ok" button, then close the dialog box.
  267.  
  268. ****************************************************************************/
  269.  
  270. static TAGTEMPLATE temps[] = {
  271. { MAKEINTRESOURCE(IDD_TAB_ONE),"Tab One" },
  272. { MAKEINTRESOURCE(IDD_TAB_TWO), "Tab Two" }
  273. };
  274.  
  275. LRESULT __export CALLBACK About(hDlg, message, wParam, lParam)
  276. HWND hDlg;               /* window handle of the dialog box */
  277. unsigned message;        /* type of message                 */
  278. WORD wParam;             /* message-specific information    */
  279. LONG lParam;
  280. {
  281.     switch (message)
  282.     {
  283.         case WM_INITDIALOG:            /* message: initialize dialog box */
  284.               BuildTabs(hDlg,hInst, IDC_TABFRAME,(TAGTEMPLATE FAR *)temps,
  285.                 sizeof(temps)/sizeof(TAGTEMPLATE)); 
  286.               SetTabStyle(hDlg,~TAB_SHADOWTEXT);              
  287.             return (TRUE);
  288.  
  289.         case WM_COMMAND: 
  290.             switch(wParam)
  291.             {
  292.              case IDOK:
  293.              case IDCANCEL:
  294.              EndDialog(hDlg, TRUE); /* Exits the dialog box        */
  295.              return (TRUE);        
  296.             }              
  297.         break;
  298.  
  299.         case WM_CTLCOLOR:
  300.          if (HIWORD(lParam) >= CTLCOLOR_LISTBOX)
  301.           SetBkColor((HDC)wParam,RGB(192,192,192));
  302.          return (LRESULT)GetStockObject(LTGRAY_BRUSH);
  303.         break;
  304.  
  305.         
  306.         default:
  307.         break;
  308.     }
  309.     return (FALSE);               /* Didn't process a message    */
  310. }